﻿using PropertyChanged;
using System;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using WPFPractice.Utils;
using WPFTemplateLib.UserControls;
using WPFTemplateLib.WpfHelpers;
using ConsoleWriter = DotNet.Utilities.ConsoleHelper.ConsoleWriter;

namespace WPFTemplate.ViewModels
{
    [AddINotifyPropertyChangedInterface]
    public class MainWindowViewModel : MyBindableBase
    {
        #region Bindable

        /// <summary>
        /// 配置
        /// </summary>
        public ConfigItems Configs { get; set; }

        //private string _Info = "";
        ///// <summary>
        ///// 信息窗内容;
        ///// </summary>
        //public string Info
        //{
        //    get => _Info;
        //    set
        //    {
        //        //保证 选择不自动滚动消息 的效果（通过不通知界面实现）,不这样的话新消息来的时候，光标/滚动条 会跳到最上面.
        //        if (Configs.IsAutoScroll)
        //        {
        //            SetPropertyWithoutCompare(ref _Info, value);
        //        }
        //        else
        //        {
        //            _Info = value;
        //        }
        //    }
        //}

        /// <summary>
        /// 底部状态栏信息
        /// </summary>
        public string Status { get; set; } = "";

        /// <summary>
        /// 弹窗VM;
        /// </summary>
        public ConfirmBoxViewModel DialogVm { get; set; } = new ConfirmBoxViewModel();

        public string UserName { get; set; }

        public string Password { get; set; }

        /// <summary>
        /// 用户ID
        /// </summary>
        public string UserId { get; set; }

        /// <summary>
        /// 发送的内容
        /// </summary>
        public string ChatContent { get; set; }

        #endregion

        public MainWindowViewModel()
        {
            Console.SetOut(new ConsoleWriter(s =>
            {
                ShowInfoUc(s);
            }));

            SetCommandMethod();

            LoadConfigCommand.Execute(false);

            ShowInfoUc($"WPF Template");
        }

        #region Command

        /// <summary>
        /// 保存配置命令
        /// </summary>
        public ICommand SaveConfigCommand { get; set; }

        /// <summary>
        /// 载入配置命令
        /// </summary>
        public ICommand LoadConfigCommand { get; set; }

        /// <summary>
        /// 关于弹窗命令
        /// </summary>
        public ICommand AboutCommand { get; set; }

        /// <summary>
        /// 弹窗演示命令
        /// </summary>
        public ICommand DialogCommand { get; set; }

        /// <summary>
        /// 双列信息弹窗演示命令
        /// </summary>
        public ICommand TwoColumnInfoCommand { get; set; }

        /// <summary>
        /// 单列信息弹窗演示命令
        /// </summary>
        public ICommand OneColumnInfoCommand { get; set; }

        /// <summary>
        /// 输入信息弹窗演示命令
        /// </summary>
        public ICommand InputInfoCommand { get; set; }

        /// <summary>
        /// 等待弹框演示命令
        /// </summary>
        public ICommand WaitCommand { get; set; }

        public ICommand LoginCommand { get; set; }

        public ICommand AddFriendCommand { get; set; }

        public ICommand SendMessageCommand { get; set; }

        /// <summary>
        /// 选择文件夹命令
        /// </summary>
        public ICommand ChooseFolderCommand { get; set; }

        #endregion

        /// <summary>
        /// 命令方法赋值(在构造函数中调用)
        /// </summary>
        private void SetCommandMethod()
        {
            SaveConfigCommand ??= new RelayCommand(o => true, async o =>
            {
                if (ConfigManager.SaveConfig(Configs))
                {
                    await ConfirmBoxHelper.ShowMessage(DialogVm, "保存配置成功");
                }
                else
                {
                    await ConfirmBoxHelper.ShowMessage(DialogVm, "保存配置失败");
                }
            });

            LoadConfigCommand ??= new RelayCommand(o => true, async o =>
            {
                var result = ConfigManager.LoadConfig<ConfigItems>();
                Configs = result.Value;
                if (result.IsSucceed)
                {
                    if (o as bool? != false)
                    {
                        await ConfirmBoxHelper.ShowMessage(DialogVm, "载入配置成功");
                    }
                }
                else
                {
                    await ConfirmBoxHelper.ShowMessage(DialogVm, "载入配置失败");
                }
            });

            AboutCommand ??= new RelayCommand(o => true, o =>
            {
                new AboutWindow().ShowDialog();
            });

            DialogCommand ??= new RelayCommand(o => true, async o =>
            {
                await ConfirmBoxHelper.ShowMessage(DialogVm, "操作前通知", 6);

                await ConfirmBoxHelper.ShowConfirm(DialogVm, "您确定要进行此操作吗？", async () =>
                {
                    #region 业务方法

                    ShowInfo("开始业务操作...");

                    await Task.Delay(1000 * 3);

                    ShowInfo("完成业务操作.");

                    #endregion

                }, msg => { ShowInfo(msg); });

                await ConfirmBoxHelper.ShowMessage(DialogVm, "操作后通知");
            });

            TwoColumnInfoCommand ??= new RelayCommand(o => true, async o =>
            {
                int leftNameWidth = 50;
                int rightNameWidth = 120;

                DialogVm.CustomContent = new UC_CustomInfo()
                {
                    DataContext = new CustomInfoViewModel()
                    {
                        TitleLeft = "个人信息",
                        TitleRight = "信用额度",
                        TextBottom = "确认继续操作吗？",

                        LeftContent = new StackPanel
                        {
                            Children =
                            {
                                GetControl.GetLineInfo("姓名:", "无名氏", Colors.Red, leftNameWidth),
                                GetControl.GetLineInfo("卡号:", "A001", Colors.Red, leftNameWidth),
                                GetControl.GetLineInfo("业务:", "消费", Colors.Red, leftNameWidth),
                                GetControl.GetLineInfo("借款:", "$100", Colors.Red, leftNameWidth)
                            }
                        },

                        RightContent = new StackPanel
                        {
                            Children =
                            {
                                GetControl.GetLineInfo("总信用额度:", "$1000", Colors.Red, rightNameWidth),
                                GetControl.GetLineInfo("已借款金额:", "$100", Colors.Red, rightNameWidth),
                                GetControl.GetLineInfo("剩余额度:", "$900", Colors.Red, rightNameWidth),
                            }
                        },
                    }
                };

                await ConfirmBoxHelper.ShowConfirm(DialogVm, "", async () =>
                {

                }, msg => { ShowInfo(msg); }, isShowText: false);
            });

            OneColumnInfoCommand ??= new RelayCommand(o => true, async o =>
            {
                int leftNameWidth = 50;

                DialogVm.CustomContent = new UC_CustomInfo()
                {
                    DataContext = new CustomInfoViewModel()
                    {
                        IsOnlyOneColumn = true,
                        TitleLeft = "标题",
                        //IsShowTitle = false,
                        //IsShowBottom = false,

                        LeftContent = new StackPanel
                        {
                            Children =
                            {
                                GetControl.GetLineInfo("姓名:", "无名氏", Colors.Red, leftNameWidth),
                                GetControl.GetLineInfo("卡号:", "A001", Colors.Red, leftNameWidth),
                                GetControl.GetLineInfo("业务:", "消费", Colors.Red, leftNameWidth),
                                GetControl.GetLineInfo("借款:", "$100", Colors.Red, leftNameWidth)
                            }
                        },
                    }
                };

                await ConfirmBoxHelper.ShowConfirm(DialogVm, "", async () =>
                {
                    Console.WriteLine("用户已确认");
                }, msg => { ShowInfo(msg); }, isShowText: false);
            });

            InputInfoCommand ??= new RelayCommand(o => true, async o =>
            {
                var inputVM = new TestInputViewModel()
                {
                    IsOnlyOneColumn = true,
                    TitleLeft = "修改后请点击确定按钮",
                    //IsShowTitle = false,
                    IsShowBottom = false,
                };

                inputVM.LeftContent = new StackPanel
                {
                    Children =
                    {
                        GetControl.GetLineInput("宽度：", nameof(inputVM.InputHeight)),
                        GetControl.GetLineInput("高度：", nameof(inputVM.InputWidth)),
                        GetControl.GetLineInput("编号：", nameof(inputVM.InputNo)),
                    },
                };

                DialogVm.CustomContent = new UC_CustomInfo()
                {
                    DataContext = inputVM,
                };

                await ConfirmBoxHelper.ShowConfirm(DialogVm, "", async () =>
                {
                    Console.WriteLine($"宽度-{inputVM.InputWidth},高度-{inputVM.InputHeight},编号-{inputVM.InputNo}");
                }, msg => { ShowInfo(msg); }, isShowText: false);
            });

            WaitCommand ??= new RelayCommand(o => true, async o =>
            {
                await ConfirmBoxHelper.ShowWait(DialogVm, "正在执行业务操作...", async () =>
                {
                    await Task.Delay(1000 * 10);
                    Console.WriteLine("操作完成");
                });
            });

            ChooseFolderCommand ??= new RelayCommand(o => true, o =>
            {
                System.Windows.Forms.FolderBrowserDialog chooseFolderDialog = new System.Windows.Forms.FolderBrowserDialog();
                if (chooseFolderDialog.ShowDialog() == DialogResult.OK)
                {
                    Configs.FolderPath = chooseFolderDialog.SelectedPath;
                }
            });

            LoginCommand ??= new RelayCommand(o => !(string.IsNullOrEmpty(UserName) || string.IsNullOrEmpty(Password)), async o =>
            {
                
            });

            AddFriendCommand ??= new RelayCommand(o => true, o =>
            {
                
            });

            SendMessageCommand ??= new RelayCommand(o => true, o =>
            {
                ShowInfoUc(ChatContent);
            });
        }

        #region 辅助方法

        

        #endregion
    }
}
